function HandlePMailerAddParcel(gui, user, date, options, data) {
  this._gui = gui;
  this._user = user;
  this._date = date;
  this._options = options;
  this._data = data;
  
  this._popupBox = new ModulePopupBoxSimple();
  this._popupBox2 = new ModulePopupBox();
  this._companys = this._data['courier'].data;
  this._models = this._data['pz_model'];
  this._states = this._data['pz_model_state'];
  this._transitions = this._data['pz_model_transition'];
  this._task = pz_task.instance();
  this._purchases = [];

  this._createElements();
};

HandlePMailerAddParcel.prototype._createElements = function() {
  var _self = this;
  DOMUtils.removeChildElements(this._gui.branch);
  DOMUtils.removeChildElements(this._gui.receiver);
  DOMUtils.removeChildElements(this._gui.company);
  DOMUtils.removeTableRows(this._gui.purchasetable, 1);
  
  this._task.model_id = 2;
  this._task.state_id = 16;
  this._task.wedding_date = 0;
  this._task.received_date = 0;
  this._task.backuped_date = 0;
  this._task.chosen_date = 0
  this._task.asstretouched_date = 0;
  this._task.checked_date = 0;
  this._task.adminretouched_date = 0;
  this._task.choosedesigner_date = 0
  this._task.retouched_date = 0;
  this._task.sorted_date = 0;
  this._task.prearrange_date = 0;
  this._task.authorarranged_date = 0;
  this._task.authorreceived_date = 0;
  this._task.published_date = 0;
  this._task.confirmpublished_date = 0;
  
  this._gui.sentdate.value = (this._task.sent_date) ? SimpleDate.format(SimpleDate.fromStamp(this._task.sent_date)) : '';
  this._gui.sentdate.onclick = function() { var context = this;
                                            _self._changeDate.call(_self, this, SimpleDate.fromStamp(_self._task.sent_date), function(sd) { _self._task.sent_date = SimpleDate.toStamp(sd); context.focus(); }, false);
                                            return false;
                                          };

  this._gui.branch.options[this._gui.branch.options.length] = new Option('-');
  for (var i = 0, il = Branch.array().length; i < il; i++) {
    var branch = Branch.array()[i];
    option = new Option(branch);
    this._gui.branch.options[this._gui.branch.options.length] = option;
    if (option.text == this._task.branch) {
      this._gui.branch.selectedIndex = this._gui.branch.options.length - 1;
    }
  }
  this._gui.branch.onchange = function() { _self._task.branch = this.options[this.selectedIndex].text; };
  
  this._gui.company.options[this._gui.company.options.length] = new Option('-');
  for (var i = 0, il = this._companys.length; i < il; i++) {
    var c = this._companys[i];
    option = new Option(c.name);
    this._gui.company.options[this._gui.company.options.length] = option;
    if (option.text == this._task.company) {
      this._gui.company.selectedIndex = this._gui.company.options.length - 1;
    }
  }
  this._gui.company.onchange = function() { _self._task.company = this.options[this.selectedIndex].text; };
  
  this._gui.addcompany.onclick = function() { var object = pz_courier.instance();
                                              var div = document.createElement('div');
                                              var pos = DOMUtils.findPos(this);
                                              var func1 = function() { _self._popupBox2._close();
                                                                       _self._retrieveCouriers.call(_self);
                                                                     };
                                              var func = function() { _self._popupBox2._close();
                                                                      _self._task.company = object.name;
                                                                      new RequestUtils()._write('pz_courier', [object], [], function() { _self._retrieveCouriers.call(_self); }, { pos: pos });
                                                                    };
                                              MiscUtils.dialog(_self._popupBox2, pos, div, func1, null, { t: 'Add Courier', width: 750, height: 50, func: func1});
                                              new ModuleDialogObject(document, div, 460, 100, _self._user, _self._date, { object: object, name: 'pz_courier', title: 'Couriers', textTitle: 'Courier', callbackFunc: func });
                                            };
  
  this._gui.code.value = this._task.code;
  this._gui.code.onchange = function() { _self._task.code = this.value };
  
  this._gui.packagenumber.value = this._task.package_number;
  this._gui.packagenumber.onchange = function() { _self._task.package_number = this.value };
  

  this._gui.comment.value = this._task.comment;
  this._gui.comment.onchange = function() { _self._task.comment = this.value };
  
  if (this._purchases.length == 0) {
    var tr = this._gui.purchasetable.insertRow(-1);
    var td = tr.insertCell(-1);
    td.colSpan = 4;
    td.style.height = '24px';
    td.style.textAlign = 'left';
    td.style.paddingLeft = '5px';
    td.appendChild(document.createTextNode('n/a'));
  }

  for (var i = 0, il = this._purchases.length; i < il; i++) {
    var task = this._purchases[i];
    var tr = this._gui.purchasetable.insertRow(-1);
    var td = tr.insertCell(-1);
    td.style.height = '24px';
    td.style.textAlign = 'left';
    td.style.paddingLeft = '5px';
    td.appendChild(document.createTextNode(task.branch + task.customer_id));
    
    td = tr.insertCell(-1);
    td.style.height = '24px';
    td.style.textAlign = 'left';
    td.style.paddingLeft = '5px';
    var span = document.createElement('span');
    span.style.fontWeight = 'bold';
    span.appendChild(document.createTextNode(task.pi_quantity + 'X'));
    td.appendChild(span);
    td.appendChild(document.createTextNode(PPublishUtils.customerName(task.bride_name, task.groom_name)));
    
    td = tr.insertCell(-1);
    td.style.height = '24px';
    td.style.textAlign = 'left';
    td.style.paddingLeft = '5px';
    td.appendChild(document.createTextNode(this._states[0].name));
    
    td = tr.insertCell(-1);
    td.style.height = '24px';
    td.style.textAlign = 'center';
    var update = document.createElement('img');
    update.src = 'image/edit.png';
    update.style.cursor = 'pointer';
    update._task = task;
    update.onclick = function() { var pos = DOMUtils.findPos(_self._gui.addpurchase);
                                  var div = document.createElement('div');
                                  var task = this._task;
                                  var func1 = function() { if (task.customer_id == '') {
                                                             window.alert('Customer No. Can Not Be Empty!');
                                                             return;
                                                           }
                                                           if (task.pi_name == '' || task.pi_quantity == '') {
                                                             window.alert('Name/Quantity Can Not Be Empty!');
                                                             return;
                                                           }
                                                           _self._createElements.call(_self);
                                                           _self._popupBox2._close();
                                                         };
                                  var func2 = function() { _self._popupBox2._close();
                                                           _self._createElements.call(_self);
                                                         };
                                  MiscUtils.dialog(_self._popupBox2, [pos[0], pos[1]-230], div, func1, func2, {t: 'Update Purchase', ok: 'Update', width: 500, height: 150});
                                  new ModuleDialogAddPurchase(document, div, 460, 100, _self._user, _self._date, { task: task }, _self._data);
                                  return false;
                                };
    td.appendChild(update);
    var del = document.createElement('img');
    del.src = 'image/delete.png';
    del.style.cursor = 'pointer';
    del.style.marginLeft = '5px';
    del._task = task;
    del.onclick = function() { MiscUtils.remove(_self._purchases, this._task, 'track_id');
                               _self._createElements.call(_self);
                             };
    td.appendChild(del);
  }
  
  this._gui.addpurchase.onclick = function() { var pos = DOMUtils.findPos(_self._gui.addpurchase);
                                               var div = document.createElement('div');
                                               var task = pz_task.instance();
                                               task.parent_trackId = _self._task.track_id;
                                               task.branch = _self._task.branch;
                                               task.sent_date = _self._task.sent_date;
                                               task.state_id = 1;
                                               task.model_id = 1;
                                               var func1 = function() { if (task.customer_id == '') {
                                                                          window.alert('Customer No. Can Not Be Empty!');
                                                                          return;
                                                                        }
                                                                        if (task.pi_name == '' || task.pi_quantity == '') {
                                                                          window.alert('Name/Quantity Can Not Be Empty!');
                                                                          return;
                                                                        }
                                                                        _self._purchases[_self._purchases.length] = task;
                                                                        _self._createElements.call(_self);
                                                                        _self._popupBox2._close();
                                                                      };
                                               var func2 = function() { _self._popupBox2._close();
                                                                        _self._createElements.call(_self);
                                                                      };
                                               MiscUtils.dialog(_self._popupBox2, [pos[0], pos[1]-230], div, func1, func2, {t: 'Add New Purchase', width: 500, height: 150});
                                               new ModuleDialogAddPurchase(document, div, 460, 100, _self._user, _self._date, { task: task }, _self._data);
                                               return false;
                                             };
  
  this._gui.add.onclick = function() { var r = window.confirm('Really To Add This Mail?');
                                       if (r) {
                                         var pos = DOMUtils.findPos(this);
                                         var tasks = []; events = []; tracks = [];
                                         _self._task.deadline = parseInt(_self._date) + 86400 * 7;
                                         tasks.push(_self._task);
                                         for (var j = 0, jl = _self._purchases.length; j < jl; j++) {
                                           var tk = _self._purchases[j];
                                           tasks[tasks.length] = tk;
                                         }
                                         var func = function() { var div = document.createElement('div');
                                                                 div.appendChild(document.createTextNode('Added Successfully!'));
                                                                 var func1 = function() { _self._popupBox._close();
                                                                                          location.reload();
                                                                                        };
                                                                 MiscUtils.dialog(_self._popupBox, pos, div, func1, null, null);
                                                               }
                                         new RequestUtils()._write2([{ n: 'pz_task', a: tasks, r: [] }], func, { pos: pos });
                                       }
                                     };
};

HandlePMailerAddParcel.prototype._changeDate = function(label, currentDate, onChangeFunc, showTime) {
  var _self = this;
  this._wContainer = document.createElement('div');
  this._wContainer.style.left = DOMUtils.findPos(label)[0] + 'px';
  this._wContainer.style.top = DOMUtils.findPos(label)[1] + 'px';
  this._popupBox._open(this._wContainer, { pos: DOMUtils.findPos(label) });
  
  var ds = new DateSelect(currentDate, null, { container: this._wContainer, showTime: showTime });
  ds._selectFunc = function(sd) { _self._selectDate.call(_self, sd, label, onChangeFunc, showTime); };
};

HandlePMailerAddParcel.prototype._selectDate = function(sd, label, onChangeFunc, showTime) {
  
  onChangeFunc(sd);
  this._showDate(sd, label, showTime);
  this._popupBox._close();
};

HandlePMailerAddParcel.prototype._showDate = function(sd, label, showTime) {
  if (sd) { 
    label.value = SimpleDate.format(sd, showTime);
  } else {
    label.value = '';
  }
};

HandlePMailerAddParcel.prototype._retrieveCouriers = function() {
  var _self = this;
  var args = '';
  new RequestUtils()._mysql('getCouriers', args, function(result, params) { _self._companys = result;
                                                                            _self._createElements();
                                                                          },  null);
};